import React from 'react';
import {
  View,
  Image,
} from 'react-native';

import Config from '../../config';
import SizeUtils from '../../common/SizeUtils';
import CommonStyles from '../../common/CommonStyles';
import TRefreshComponent from '../../components/TRefreshComponent';
import BaseComponent from '../common/BaseComponent';
import TImage from '../../components/TImage';
import QZ_RequestQuanziListAction from '../../actions/quanzi/QZ_RequestQuanziListAction';

export default class CommonDataListView extends BaseComponent {
  constructor(props) {
    super(props);

    this.state = {
      data: {},
      isResponeUpdate: false,
      viewStatus: -1,
    }
    this._noEmpty = this.props.noEmpty;
    this._useLoadMore = this.props.useLoadMore;
    this._pullRefresh = null;
    this._authorization = undefined;
    this._itemClickEnable = this.props.itemClickEnable === null ? true : this.props.itemClickEnable;
    this._dataName = this.props.dataName ? this.props.dataName : "data";
    this._updateKey = this.props.updateKey ? this.props.updateKey : "isResponeUpdate";
    this._dataKey = this.props.dataKey ? this.props.dataKey : undefined;//数据的key 比如说 newsData里面的newsId
  }

  _dealComponentWillUpdate(nextProps,nextState) {
    if(nextState[this._updateKey] === true) {
      console.log("shouldComponentUpdate ResponeUpdate", nextState[this._dataName]);
      nextState[this._updateKey] = false;
      this.finish(1, nextState[this._dataName]);
    }
  }

  render() {
    return (
      <View style={[this._getStyle(), {backgroundColor:"#00000000"}]}>
        {this._renderEmpty()}
        <TRefreshComponent style={{width:this._getStyle().width, height:this._getStyle().height}}
          ref = {(ref) => {this._pullRefresh = ref}}
          onRenderHeader = {this._onRenderHeader.bind(this)}
          onRefresh = {this._onRefresh.bind(this)}
          onPressItem = {this._onPressItem.bind(this)}
          onRenderItem = {this._onRenderItem.bind(this)}
          onRenderFooter = {this._renderFooter.bind(this)}
          autoInitRequest = {this.props.autoInitRequest}
          useLoadMore = {this._useLoadMore}
          isTR={this.props.isTR}
          useRefresh = {this.props.useRefresh}
          itemClickEnable = {this._itemClickEnable}
          onScroll = {this._onScroll.bind(this)}
          isRowWrap = {this.props.isRowWrap}
          dataKey = {this._dataKey}
        />
        {this._renderBottomView()}
      </View>
    )
  }
  setRefresh(){
    this._pullRefresh._refreshing=false;
  }
  scrollTo(params) {
    if(this._pullRefresh) {
      this._pullRefresh.scrollTo(params);
    }
  }

  _onScroll(event) {
    if(this.props.onScroll) {
      this.props.onScroll(event);
    }
  }

  clear() {
    this._pullRefresh.clear();
  }
  leave() {
    this._pullRefresh.leave();
  }
  _renderBottomView() {

  }

  _renderFooter() {
    if(this.props.onRenderFooter) {
      return this.props.onRenderFooter();
    }
  }

  _onRenderHeader() {
    if(this.props.onRenderHeader) {
      return this.props.onRenderHeader();
    }
    else {
      //要不然不显示下拉刷新
      return (
        <View style={{height:1}}>
        </View>
      )
    }
  }

  _onRenderItem(data, index) {
    return this.props.onRenderItem(data, index);
  }

  _getStyle() {
    return this.props.style;
  }
  _getHeader(){
    return this.props.headerHeight||0;
  }
  doRefresh() {
    this._pullRefresh.doRefresh();
  }

  finish(type, data, params) {
    this._pullRefresh && this._pullRefresh.finish(type, data, params);
  }

  _renderEmpty() {
    if((this._pullRefresh && this._pullRefresh.getData().length > 0) || this._noEmpty === true) {
      return;
    }
    var y = (this._getStyle().height-this._getHeader()- SizeUtils.translateDesignWidthToCode(220))/2+this._getHeader();
    return (
      <View style={{width:this._getStyle().width||SizeUtils.getWindowSize().width, flexDirection:"row", justifyContent:"center",
        backgroundColor:"#ffffff",alignItems:"center",
        position:'absolute',
         left:this._getStyle().marginLeft, top:y}}>
        <TImage source={require("../../../resource/images/common/weifabu.png")} style={{
          width:220, height:220,
        }} />
      </View>
    )
  }

  _getOpacity() {
    return (this._pullRefresh && this._pullRefresh.getData().length == 0) ? 1 : 0;
  }

  _onRefresh(page) {
    this._requestData(page);
  }

  _onPressItem(data) {
    this.props.onPressItem && this.props.onPressItem(data);
  }

  _requestData(page) {
    this.props.requestData(page, this);
  }
}
